﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1b
{
    public partial class Form1 : Form
    {
        private int totalMinute=2;
        private int totalSeconds=0;
        private int localSeconds=14;
        private int problemNum = 0;
        private int score = 0;
        private int num1;
        private int num2;
        private int results;
        private String opt;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try {
                if (Convert.ToInt32(result.Text) == results)
                {
                    score += 10;
                    problemNum++;
                    if (problemNum == 10) {
                        num.Text = problemNum + "/10";
                        return;
                    }
                    Next();
                    localSeconds = 14;
                }
                else
                {
                    MessageBox.Show("算错了!");
                }
            }
            catch (Exception ex) {
                MessageBox.Show("结果应为整数。");
            }
            num.Text = problemNum + "/10";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ensure.Visible = true;
            begin.Visible = false;
            Start();
        }

        private void Start() {
            total.Interval = 1000;
            total.Start();
            total.Tick += Total_Tick;
            local.Interval = 1000;
            local.Start();
            local.Tick += Local_Tick;
            Next();
        }

        private void Next() {
            result.Text = "";
            Random rd = new Random();
            num1 = rd.Next(100);
            num2 = rd.Next(100);
            int o = rd.Next(2);
            switch (o) {
                case 0: { 
                        opt = "+";
                        results = num1 + num2;
                        break; }
                case 1: { 
                        opt = "-";
                        if (num1 < num2)
                        {
                            int temp = num1;
                            num1 = num2;
                            num2 = num1;
                        }
                        results = num1 - num2;
                        break; }
            }
            question.Text = num1 + opt + num2+"=";
        }
        private void Local_Tick(object sender, EventArgs e)
        {
            if (localSeconds == 0) {
                Next();
                localSeconds = 14;
                problemNum++;
                num.Text = problemNum + "/10";
                return;
            }
            localSeconds--;
            if (problemNum == 10) {
                total.Stop();
                local.Stop();
                MessageBox.Show("游戏结束。得分：" + score);
            }
        }

        private void Total_Tick(object sender, EventArgs e)
        {
            if (totalSeconds == 0)
            {
                if (totalMinute == 0)
                {
                    total.Stop();
                    local.Stop();
                    MessageBox.Show("游戏结束。得分："+score);
                    return;
                }
                totalSeconds = 60;
                totalMinute--;
            }
            totalSeconds--;
            totalTime.Text = totalMinute+":"+totalSeconds;
        }
    }
}
